package org.southyin.service.impl;

import org.southyin.commons.CommonsPage;
import org.southyin.commons.CommonsResp;
import org.southyin.mapper.CmsArticleMapper;
import org.southyin.model.CmsArticle;
import org.southyin.service.CmsArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CmsArticleServiceImpl implements CmsArticleService {
    @Autowired
    CmsArticleMapper mapper;
    public CommonsResp saveArticleInfo(CmsArticle cmsArticle) {
        int i = mapper.saveArticleInfo(cmsArticle);
        if (i == 0) {
            return CommonsResp.failed();
        }
        return CommonsResp.succeess();
    }

    public CommonsPage<CmsArticle> listHotOrNewOrHotList(String listId, int pageNum, int pageSize) {
        List<CmsArticle> cmsArticles = mapper.listHotOrNewOrHotList(listId,(pageNum-1)*pageSize,pageSize);
        int total = mapper.getTotalCounsOfArticles(null);
        CommonsPage<CmsArticle> commonsPage = new CommonsPage<CmsArticle>();
        commonsPage.setTotal(total);
        commonsPage.setList(cmsArticles);
        return commonsPage;
    }

    public CmsArticle getArticleById(Long id) {
        CmsArticle article = mapper.getArticleById(id);
        if (article != null) {
            mapper.updateArticleViews(id);
            return article;
        }
        return null;
    }
}
